package com.borqsprocessmanager.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;
import android.os.Handler;
import android.os.Message;

import com.borqsprocessmanager.util.Log;

public class ColorTextView extends TextView {
    private final String TAG = "ColorTextView";

    private boolean isControl = true;
    private String touchColor = "#178ab4";
    private String upColor = "#9a9a9a";

    public ColorTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public ColorTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public ColorTextView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public void setControlTextColor(boolean isControl) {
        this.isControl = isControl;
    }

    private void createhandler() {
        final Handler handler = new Handler() {
            public void handleMessage(Message msg) {
                setTextColor(Color.parseColor(upColor));
            }
        };
        handler.sendEmptyMessageDelayed(0, 150);

    }

    // public void setColor(int touchColor, int upColor) {
    // this.touchColor = touchColor;
    // this.upColor = upColor;
    // }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isControl) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.v(TAG, "ACTION_DOWN");
                setTextColor(Color.parseColor(touchColor));
                // invalidate();
                break;
            case MotionEvent.ACTION_UP:
                Log.v(TAG, "ACTION_UP");
                createhandler();
                // setTextColor(Color.parseColor(upColor));
                // invalidate();
                break;
            case MotionEvent.ACTION_CANCEL:
                setTextColor(Color.parseColor(upColor));
                break;

            default:

                break;
            }
        }
        return super.onTouchEvent(event);
    }
}
